#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import os
from ..._util import Util, CopiedAndSymlinkedDir
from ..._game import GameBase, GameSpec, MatchContext
from ..._game_straits import GameStraitForWindows, GameStraitNeedConfigDir


class CallOfDuty2(GameBase, GameStraitForWindows):

    def __init__(self, data_dir):
        GameBase.__init__(self, data_dir)

        self._addStrait(GameStraitNeedConfigDir())

        self._spec = GameSpec()
        if True:
            self._spec.windows_archs = [self.WindowsArch.WIN32]
            self._spec.windows_versions = [self.WindowsVersion.WINXP, "*"]

            self._spec.default_language = "en_US"
            self._spec.languages = [self._spec.default_language]

            # self._spec.resolutions = ["640x480", "800x600", "1024x768", "1280x1024", "1600x1200", "2048x1536"]
            self._spec.resolutions = ["640x480"]
            self._spec.can_fullscreen = True
            self._spec.can_windowed = False

    @property
    def spec(self):
        return self._spec

    @property
    def run_items(self):
        return ["sp", "mp"]

    def get_title(self, lang):
        return "Call of Duty"

    def _prepare(self, context):
        self._context = context
        try:
            self._targetGameDirObj = CopiedAndSymlinkedDir(os.path.join(self._context.drive_c_dirpath, "Call of Duty 2"), [self._dataDir])
            self._targetGameDirObj.symlink("", recursive=True)
            self._targetGameDirObj.clear()
        except BaseException:
            self._cleanup(on_error=True)
            raise

    @MatchContext.edit_or_run
    def get_language(self):
        return self._spec.default_language

    @MatchContext.edit_or_run
    def is_music_enabled(self):
        self._gameIniFile.get_option("music-enabled")

    @MatchContext.edit_or_run
    def is_sfx_enabled(self):
        self._gameIniFile.get_option("sfx-enabled")

    @MatchContext.edit_or_run
    def set_language(self, language):
        assert language == self._spec.default_language

    @MatchContext.run
    def apply_resolution(self, resolution):
        assert resolution in self._spec.resolutions

    @MatchContext.run
    def apply_fullscreen_or_windowed(self, fullscreen_or_windowed):
        assert fullscreen_or_windowed

    def _commit(self):
        pass

    def _getRunInfo(self, item):
        assert not self._context.edit_or_run

        if item == "sp":
            return self.RunInfo("C:\\Call of Duty 2\\CoD2SP_s.exe", cwd="C:\\Call of Duty 2")
        elif item == "mp":
            return self.RunInfo("C:\\Call of Duty 2\\CoD2MP_s.exe", cwd="C:\\Call of Duty 2")
        else:
            assert False

    def _cleanup(self, on_error=False):
        if hasattr(self, "_targetGameDirObj"):
            if not on_error:
                pass
            del self._targetGameDirObj
        if hasattr(self, "_context"):
            del self._context
